package com.play.ballen.me.ui

import android.content.Context
import android.content.Intent
import android.widget.Button
import androidx.recyclerview.widget.LinearLayoutManager
import com.hjq.toast.ToastUtils
import com.play.ballen.R
import com.play.ballen.databinding.ActivityClaimedBinding
import com.play.ballen.me.adapter.ClaimedAdapter
import com.play.ballen.me.vm.ClaimedVM
import com.scwang.smart.refresh.layout.api.RefreshLayout
import com.scwang.smart.refresh.layout.listener.OnRefreshLoadMoreListener
import com.yuven.appframework.base.BaseStdTitleActivity
import com.yuven.baselib.utils.ResourceUtil
import com.yuven.baselib.widget.MultiStateView

class ClaimedActivity : BaseStdTitleActivity<ClaimedVM, ActivityClaimedBinding>() {

    private val adapter: ClaimedAdapter by lazy {
        ClaimedAdapter(arrayListOf())
    }

    companion object {
        @JvmStatic
        fun start(context: Context) {
            val starter = Intent(context, ClaimedActivity::class.java)
            context.startActivity(starter)
        }
    }

    override fun viewModelClass(): Class<ClaimedVM> {
        return ClaimedVM::class.java
    }

    override fun contentLayoutId(): Int {
        return R.layout.activity_claimed
    }

    override fun getToolBarTitle(): String {
        return ResourceUtil.getString(R.string.me_to_be_claimed)
    }

    override fun initView() {
        super.initView()
        dataBinding.msvLayout.findViewById<Button>(R.id.btnTryAgain).setOnClickListener {
            dataBinding.msvLayout.viewState = MultiStateView.VIEW_STATE_LOADING
            viewModel.claimedLog()
        }
    }

    override fun initObserver() {
        super.initObserver()
        viewModel.actionGetListFailure.observe(this) {
            dataBinding.refreshLayout.finishRefresh()
            dataBinding.refreshLayout.finishLoadMore()
            if (viewModel.page == 1) {
                dataBinding.msvLayout.viewState = MultiStateView.VIEW_STATE_ERROR
            } else {
                ToastUtils.show(ResourceUtil.getString(R.string.load_fail))
            }
        }

        viewModel.listLog.observe(this) {
            dataBinding.refreshLayout.finishRefresh()
            dataBinding.refreshLayout.finishLoadMore()

            if (!it.isNullOrEmpty()) {
                if (viewModel.page == 1) {
                    adapter.setList(it)

                    dataBinding.msvLayout.viewState = MultiStateView.VIEW_STATE_CONTENT
                } else {
                    adapter.addData(it)
                }
                viewModel.page++
            } else {
                //no data
                if (viewModel.page == 1) {
                    dataBinding.msvLayout.viewState = MultiStateView.VIEW_STATE_EMPTY
                } else {
                    ToastUtils.show(ResourceUtil.getString(R.string.load_end))
                }
            }
        }
    }

    override fun initData() {
        dataBinding.refreshLayout.setEnableLoadMore(true)
        dataBinding.refreshLayout.setOnRefreshLoadMoreListener(object : OnRefreshLoadMoreListener {
            override fun onRefresh(refreshLayout: RefreshLayout) {
                viewModel.page = 1
                viewModel.claimedLog()
            }

            override fun onLoadMore(refreshLayout: RefreshLayout) {
                viewModel.claimedLog()
            }
        })

        dataBinding.rv.layoutManager = LinearLayoutManager(mContext)
        dataBinding.rv.adapter = adapter

        dataBinding.msvLayout.viewState = MultiStateView.VIEW_STATE_LOADING
        viewModel.page = 1
        viewModel.claimedLog()
    }

}